package cn.shenlong.front.directive.abs;

import java.util.Map;

import cn.shenlong.util.DirectiveUtils;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 内容标签基类，只负责取数据
 */
public abstract class AbstractContentDirective implements
		TemplateDirectiveModel {

	/**
	 * 输入参数，是否去掉标题重复的内容，0否，1是，不填写默认否。
	 */
	public static final String PARAM_TITLE_REPEAT = "titleRepeat";
	/**
	 * 输入参数，栏目ID。允许多个栏目ID，用","分开。和channelPath之间二选一，ID优先级更高。
	 */
	public static final String PARAM_CHANNEL_ID = "channelId";

	/**
	 * 输入参数，类型ID。可选。允许多个类型ID,用","分开。
	 */
	public static final String PARAM_TYPE_ID = "typeId";

	/**
	 * 待定
	 */
	public static final String PARAM_ORDER_BY = "orderBy";
	/**
	 * 输入参数，不包含的文章ID。用于按tag查询相关文章。
	 */
	public static final String PARAM_EXCLUDE_ID = "excludeId";

	protected Integer getTitleRepeat(Map<String, TemplateModel> params)
			throws TemplateException {
		return DirectiveUtils.getInt(PARAM_TITLE_REPEAT, params);
	}

	protected Long[] getChannelIds(Map<String, TemplateModel> params)
			throws TemplateException {
		Long[] ids = DirectiveUtils.getLongArray(PARAM_CHANNEL_ID, params);
		if (ids != null && ids.length > 0) {
			return ids;
		} else {
			return null;
		}
	}

	protected Long[] getTypeIds(Map<String, TemplateModel> params)
			throws TemplateException {
		Long[] typeIds = DirectiveUtils.getLongArray(PARAM_TYPE_ID, params);
		return typeIds;
	}

	protected int getOrderBy(Map<String, TemplateModel> params)
			throws TemplateException {
		Integer orderBy = DirectiveUtils.getInt(PARAM_ORDER_BY, params);
		if (orderBy == null || orderBy < 0 || orderBy > 23) {
			// 默认为2：按文章发布时间排序
			return 2;
		} else {
			return orderBy;
		}
	}

	protected Object getData(Map<String, TemplateModel> params, Environment env)
			throws TemplateException {

		Integer orderBy = getOrderBy(params);

		return null;
	}

	abstract protected boolean isPage();

}
